import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:robot3/pojo.dart';
import 'package:robot3/services.dart';
import 'package:robot3/viewmodel/CustomersProvider.dart';
import 'package:robot3/widgets.dart';

import 'backButton1.dart';

//客户管理
void main() {
  runApp(MaterialApp(home: const Name1()));
}

class Name1 extends StatelessWidget {
  const Name1({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return
    ChangeNotifierProvider(
      create: (_) => CustomersProvider(),
      child: _ShangpinkuScreen(title: 'Flutter Demo Home Page'),
    );
    // );
  }
}

class _ShangpinkuScreen extends StatefulWidget {
  const _ShangpinkuScreen({super.key, required this.title});

  final String title;

  @override
  State<_ShangpinkuScreen> createState() => _PageState();
}

class _PageState extends State<_ShangpinkuScreen> {
  static const platform = MethodChannel('com.abc.robot01.xiangqing');

  // String  accessToken="ed8a4b93-873d-42eb-85e7-19bf74cbb053";
  String id = "1bdc4e2b35764b46b133481f0807e5d6";
  String? name = "";

  @override
  void initState() {
    super.initState();
    final provider = Provider.of<CustomersProvider>(context, listen: false);
    provider.login().then((value) {
      if (value) {
        provider.getCustomers().then((value) {
          // final name = provider.list.first.customerName;
          // final phone = provider.list.first.customerPhone;
          // print(provider.list.first.customerName);
          // print(provider.list.first.customerPhone);
          // provider
          //     .getCustomerDetails(
          //       name,
          //       phone,
          //       provider.list.first.renderTotalAmount,
          //     )
          //     .then((value) {});
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // Image equivalent to the Image composable
          // Image.asset(
          //   'images/bg.png', // 替换为你的图片路径
          //   fit: BoxFit.cover,
          //   width: double.infinity,
          //   height: double.infinity,
          // ),
          Padding(
            padding: const EdgeInsets.only(
              left: 30 / ratio,
              right: 30 / ratio,
              top: 30 / ratio,
            ),
            child: // 加载完成后显示商品信息
                Content01(child: Content()),
          ),
          // Positioned(
          //   left: 30 / ratio,
          //   bottom: 30 / ratio,
          //   child: BackButton1(),
          // ),
        ],
      ),
    );
  }
}

class Content01 extends StatelessWidget {
  final Widget child;

  const Content01({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return
    Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 10 / ratio),
        Expanded(child: child),
      ],
      // ),
    );
  }
}

class Info extends StatefulWidget {
  const Info({super.key});
  @override
  _InfoState createState() => _InfoState();
}

class _InfoState extends State<Info> {
  final TextEditingController phoneController = TextEditingController();
  final TextEditingController nameController = TextEditingController();
  final TextEditingController nameController2 = TextEditingController();

  @override
  void dispose() {
    // Always dispose of controllers when the widget is disposed
    phoneController.dispose();
    nameController.dispose();
    nameController2.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
   return Padding(
      padding: EdgeInsets.only(top: 30 / ratio, bottom: 19 / ratio),
      child: Row(
        children: [
          Text(
            "用户手机",
            style: TextStyle(
                color: MyBlack,
                fontSize: 24 / ratio, fontWeight: FontWeight.normal),
          ),
          SizedBox(width: 20 / ratio),

          SizedBox(width: 374/ratio,
              height: 44/ratio,
              child:
              RobotEditText(
                  keyboardType:TextInputType.number,
                  controller:phoneController,onTextChanged: (text){})
          ),

          SizedBox(width: 30 / ratio),
          Text(
            "用户姓名",
            style: TextStyle(
                color: MyBlack,
                fontSize: 24 / ratio,
                fontWeight: FontWeight.normal),
          ),
          SizedBox(width: 20 / ratio),
          SizedBox(width: 374/ratio,
              height: 44/ratio,
              child:
              RobotEditText(controller:nameController,
                  onTextChanged: (text){})
          ),

          SizedBox(width: 30 / ratio),
          Text(
            "状态",
            style: TextStyle(
                color: MyBlack,
                fontSize: 24 / ratio,
                fontWeight: FontWeight.normal),
          ),
          SizedBox(width: 20 / ratio),
          SizedBox(width: 374/ratio,
              height: 44/ratio,
              child:GestureDetector(
                  onTap: (){
                    print("1");
                  },
                  child:
              RobotEditText(enabled:false,controller:nameController2,onTextChanged: (text){}))
          ),
          SizedBox(width: 47 / ratio),
          ConfirmButton(onClick: (text){
            print(phoneController.text);
            print(nameController.text);
            Provider.of<CustomersProvider>(context,listen: false).getCustomers(name: nameController.text,phone: phoneController.text);
          },text: "查询",width: 148/ratio,),
        ],
      ),
    );
  }
}

///Title
class Title extends StatelessWidget {
  const Title({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: MyBlack.withOpacity(0.05),
      child: Padding(
        padding: EdgeInsets.only(
          top: 16 / ratio,
          bottom: 16 / ratio,
          left: 14 / ratio,
          right: 14 / ratio,
        ),
        child: Row(
          children: [
            Text(
              "用户手机",
              style: TextStyle(color: MyBlack, fontSize: 24 / ratio),
            ),
            SizedBox(width: 141 / ratio),
            Text(
              "用户姓名",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),
            SizedBox(width: 120 / ratio),
            Text(
              "用户状态",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),

            SizedBox(width: 73 / ratio),
            Text(
              "渲染次数",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),

            SizedBox(width: 70 / ratio),
            Text(
              "渲染金额",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),
            SizedBox(width: 71 / ratio),
            Text(
              "最近渲染时间",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),
            SizedBox(width: 68 / ratio),
            Text(
              "最近渲染金额",
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack),
            ),

            Spacer(), // 添加 Spacer 将 "渲染时间" 推到最右边
            Text("操作", style: TextStyle(fontSize: 24 / ratio, color: MyBlack)),
            SizedBox(width: 168 / ratio),
          ],
        ),
      ),
    );
  }
}

class Item extends StatelessWidget {
  final Customer customer;

  const Item({super.key, required this.customer});

  @override
  Widget build(BuildContext context) {
    // final top =
    //     product.position == 3
    //         ? Radius.circular(10 / ratio)
    //         : product.position == 0
    //         ? Radius.circular(10 / ratio)
    //         : Radius.zero;
    // final bottom =
    //     product.position == 3
    //         ? Radius.circular(10 / ratio)
    //         : product.position == 2
    //         ? Radius.circular(10 / ratio)
    //         : Radius.zero;

    return Container(
      decoration: BoxDecoration(
        // borderRadius: BorderRadius.only(
        //   topLeft: top,
        //   topRight: top,
        //   bottomLeft: bottom,
        //   bottomRight: bottom,
        // ),
        color: Colors.white.withOpacity(0.40),
        border: Border(
          bottom: BorderSide(
            color: MyBlack.withOpacity(0.20), // bottom边框颜色
            width: 1 / ratio, // 顶部边框宽度
          ),
        ),
      ),
      // color: Colors.red.withAlpha(102),
      child: Padding(
        padding: EdgeInsets.only(
          top: 16 / ratio,
          bottom: 16 / ratio,
          left: 14 / ratio,
          right: 14 / ratio,
        ),
        child: Row(
          children: [
            SizedBox(
              width: 160 / ratio,
              child: Text(
                ""+customer.customerPhone,
                // "136 1478 2569",
                style: TextStyle(
                  color: MyBlack,
                  fontSize: 24 / ratio,
                  letterSpacing: 0,
                  wordSpacing: 0,
                ),
              ),
            ),
            SizedBox(width: 79 / ratio),
            SizedBox(
              width: 95 / ratio,
              child: Text(
                // "122333333333",
                customer.customerName,
                textAlign: TextAlign.right, // Aligns text to the right
                style: TextStyle(fontSize: 24 / ratio,
                    letterSpacing: 0,
                    wordSpacing: 0,
                    color: MyBlack),
              ),
            ),
            SizedBox(width: (48+85) / ratio),
            SizedBox(
              width: 96 / ratio,
              child: Text(
                "潜在客户",
                style: TextStyle(fontSize: 24 / ratio,
                    letterSpacing: 0,
                    wordSpacing: 0,
                    color: MyBlack),
              ),
            ),
            SizedBox(width: 96 / ratio),
            SizedBox(width: 68/ratio,child:
            Text(
              // "" + 10000.toString(),
              customer.renderCount.toString(),
              textAlign: TextAlign.right, // Aligns text to the right
              style: TextStyle(fontSize: 24 / ratio,
                  letterSpacing: 0,
                  wordSpacing: 0,
                  color: MyBlack),
            )),
            SizedBox(width: 60 / ratio),
            SizedBox(width: 114/ratio,child:
            Text(
              customer.renderTotalAmount.toString(),
              // "145287.00"  ,
              textAlign: TextAlign.right, // Aligns text to the right
              style: TextStyle(fontSize: 24 / ratio,
                  letterSpacing: 0,
                  wordSpacing: 0,
                  color: MyBlack),
            )
            ),

            // Spacer(), // 添加 Spacer 将 "渲染时间" 推到最右边

            SizedBox(width: 96 / ratio),
            SizedBox(width: 121/ratio,child:
            Text(
              "2023-09-23",
              textAlign: TextAlign.right, // Aligns text to the right
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack,
                letterSpacing: 0,
                wordSpacing: 0,
              ),
            )),

            SizedBox(width: 96 / ratio),
            SizedBox(width: 121/ratio,child:
            Text(
              customer.latestAmount.toString(),
              textAlign: TextAlign.right, // Aligns text to the right
              style: TextStyle(fontSize: 24 / ratio, color: MyBlack,
                letterSpacing: 0,
                wordSpacing: 0,
              ),
            )),
            SizedBox(width: 52 / ratio),

  Row(
    spacing: 17/ratio,
    children: [
      ConfirmButton(onClick: (text){},text: "客户信息",),
      ConfirmButton(onClick: (text){
        Navigator.pushNamed(
          context,
          '/customerRenderDetails',
          arguments: {
            'name': customer.customerName,
            'phone': customer.customerPhone,
            'total': customer.renderTotalAmount,
          },
        );
      },text: "渲染详情",),
  ],)
          ],
        ),
      ),
    );
  }
}

class Content extends StatelessWidget {
  const Content({super.key});

  @override
  Widget build(BuildContext context) {
    final customerList = context.watch<CustomersProvider>().customerList;
    return RobotCard(
      child: Padding(
        padding: EdgeInsets.only(left: 30 / ratio, right: 30 / ratio),
        child: SizedBox(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(20.0 / ratio),
            // 保持 CustomScrollView 圆角一致
            child: CustomScrollView(
              physics: BouncingScrollPhysics(),
              slivers: [
                SliverToBoxAdapter(child: Info()),
                SliverToBoxAdapter(child: Title()),
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                      return Item(customer: customerList[index]);
                    },
                    childCount: customerList.length, // Number of items
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
